// pages/Index/shopActive/friendCards/selectUser.js
const controllers = require('../../../controllers/controllerIndex.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: false,
    pay: false,
    activeIndex: 0,
    selectedIcon:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210810/874665118292508672.png",
    noSelectedIcon:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210810/874665097627172864.png",
    tabs: [{
        name: "全部",
        vip: null,
      },
      {
        name: "普通用户",
        vip: 0,
      },
      {
        name: "会员",
        vip: 2,
      },
      {
        name: "VIP会员",
        vip: 1, 
      },
      {
        name: "股东",
        vip: 3, 
      }
    ],
    typeList: ["手机号", "昵称"],
    typeName: "手机号",
    index: 1,
    inputText: '',
    store: "",
    pageNumber: 1,
    lastPage: false,
    selectUserList:[]
  },

  //取消按钮
  cancleBtn(){
    wx.navigateBack()
  },

  //点击确定按钮
  catchTapSubBtn(){
    let pages=getCurrentPages()
    let prevPage=pages[pages.length-2]

    let selectUserList=this.data.selectUserList
    prevPage.setData({
      selectUserList:selectUserList
    })

    wx.navigateBack()
  },

  //选择用户
  selectUser(e){
    let index=e.currentTarget.dataset.index
    let list=this.data.userList
    let selectUserList=this.data.selectUserList
    if(list[index].selected==false){
      list[index].selected=true
      selectUserList.push(list[index])
    }else{
      list[index].selected=false
      for(let i=0;i<selectUserList.length;i++){
        if(list[index].id==selectUserList[i].id){
          selectUserList.splice(i,1)
        }
      }
    }
    this.setData({
      userList:list,
      selectUserList:selectUserList
    })
  },

  //跳转提现文案设置
  toSetBeanDesc() {
    wx.navigateTo({
      url: '/pages/userStatistics/Userindex/setBeanDesc',
    })
  },

  seletBalance() {
    let pay = this.data.pay;
    this.setData({
      pay: pay ? false : true,
      isTime: false
    })
    this.getUserList()
  },

  seletTime() {
    let time = this.data.time;
    this.setData({
      time: time ? false : true,
      isTime: true
    })
    this.getUserList()
  },

  //切换名称和号码搜索
  bindTypeChange(e) {
    let index = e.detail.value;
    if (index == 0) {
      this.setData({
        index: 1,
        typeName: "手机号"
      })
    } else {
      this.setData({
        index: 0,
        typeName: "昵称"
      })
    }
  },

  //获取输入数据
  getSearchText(e) {
    this.setData({
      pageNumber:1,
      inputText: e.detail.value
    })
    this.getUserList()
  },

  getUserList() {
    let that = this
    let time = this.data.time
    let pay = this.data.pay
    let isTime = this.data.isTime
    let params = {
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId')
    }
    params.pageNumber = this.data.pageNumber
    if (this.data.index == 0) {
      params.nick = this.data.inputText
    } else {
      params.mobile = this.data.inputText
    }
    if (isTime) {
      params.time = time ? "DESC" : "ASC"
    } else {
      params.pay = pay ? "DESC" : "ASC"
    }

    let item = this.data.tabs[this.data.activeIndex];
    if (null != item.vip) {
      params.vip = item.vip;
    }

    let selectUserList=this.data.selectUserList
    

    controllers.accountListPage(
      params
    ).then(res => {
      res.data.list.forEach(item=>{
        item.selected=false
        for(let j=0;j<selectUserList.length;j++){
          if(item.id==selectUserList[j].id){
            item.selected=true
          }
        }
      })
      that.setData({
        userList: res.data.list,
        lastPage: res.data.lastPage
      })
    })
  },

  getMore() {
    if (this.data.lastPage == false) {
      let that = this
      let time = this.data.time
      let pay = this.data.pay
      let isTime = this.data.isTime
      let params = {
        sessionId: wx.getStorageSync('token'),
        shopId: wx.getStorageSync('shopId')
      }
      params.pageNumber = this.data.pageNumber + 1
      this.setData({
        pageNumber: params.pageNumber
      })
      if (this.data.index == 0) {
        params.nick = this.data.inputText
      } else {
        params.mobile = this.data.inputText
      }
      if (isTime) {
        params.time = time ? "DESC" : "ASC"
      } else {
        params.pay = pay ? "DESC" : "ASC"
      }

      let item = this.data.tabs[this.data.activeIndex];
      if (null != item.vip) {
        params.vip = item.vip;
      }

      controllers.accountListPage(
        params
      ).then(res => {

        res.data.list.forEach(item=>{
          item.selected=false
          for(let j=0;j<this.data.selectUserList.length;j++){
            if(item.id==this.data.selectUserList[j].id){
              item.selected=true
            }
          }
        })

        let list = this.data.userList
        let list2 = res.data.list
        let list3 = list.concat(list2)
        that.setData({
          userList: list3,
          lastPage: res.data.lastPage
        })
      })
    }
  },

  clikcTabs(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      pageNumber:1
    })
    this.setData({
      activeIndex: index
    });
    this.getUserList()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.storeInfo) {
      this.setData({
        storeInfo: JSON.parse(options.storeInfo),
      })
    }
    this.setData({
      selectUserList:JSON.parse(decodeURIComponent(options.list))
    })
    this.getUserList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})